package com.hangzhou.welbeing.welbeinginstrument.activity;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hangzhou.welbeing.welbeinginstrument.R;
import com.hangzhou.welbeing.welbeinginstrument.base.MyApplication;
import com.hangzhou.welbeing.welbeinginstrument.bean.IndividualitySignatureBean;
import com.hangzhou.welbeing.welbeinginstrument.config.RBConstants;
import com.hangzhou.welbeing.welbeinginstrument.utils.Utils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 时间 2017 年 09 月 21.
 * 作者 Administrator  刘传华
 * 公司 杭州惟君医疗科技有限公司
 * 包名 com.hangzhou.welbeing.pregnant.welbeing_pregnant.activity
 * 工程名 welbeing_pregnant
 */
public class IndividualitySignatureActivity extends Activity {
    private static final String TAG ="IndividualitySignatureActivity" ;
    @Bind(R.id.activity_individuality_signature_iv_return)
    ImageView activityIndividualitySignatureIvReturn;
    @Bind(R.id.activity_individuality_signature_tv_complete)
    TextView activityIndividualitySignatureTvComplete;
    @Bind(R.id.activity_individuality_signature_tv_content)
    TextView activityIndividualitySignatureTvContent;
    @Bind(R.id.activity_individuality_signature_et_content)
    EditText activityIndividualitySignatureEtContent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_individuality_signature);
        ButterKnife.bind(this);

        Utils.setStatusBar(this,false,false);
        Utils.setStatusTextColor(true,this);
    }

    @OnClick({R.id.activity_individuality_signature_iv_return, R.id.activity_individuality_signature_tv_complete})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.activity_individuality_signature_iv_return:
                finish();
                break;
            case R.id.activity_individuality_signature_tv_complete:
                String content = activityIndividualitySignatureEtContent.getText().toString().trim();
                if(!TextUtils.isEmpty(content)){
                    completeIndividualitySignature(content);
                }else{
                    Toast.makeText(this,"提交前请输入个性的签名",Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
    /**
     * @desc 提交个性签名
     * */
    private void completeIndividualitySignature(String content) {
        SharedPreferences sp = getSharedPreferences("second_app_internal_data", 0);
        String user_id = sp.getString("user_id", "");
        OkHttpUtils
                .post()
                .url(RBConstants.SERVER_URL+RBConstants.INDIVIDUALITY_SIGNATURE)
                .addParams("cus_id", user_id)
                .addParams("sig",content)
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String stringparseNetworkResponse = response.body().string();
                        Gson gson = new Gson();
                        Log.e(TAG,"stringparseNetworkResponse----setPicToView---9------->"+stringparseNetworkResponse);
                        final IndividualitySignatureBean individualitySignatureBean =  gson.fromJson(stringparseNetworkResponse,IndividualitySignatureBean.class);
                        if(individualitySignatureBean.errorCode == 200){
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(IndividualitySignatureActivity.this,"您的签名提交成功",Toast.LENGTH_SHORT).show();
                                    finish();
                                }
                            });
                        }else{
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(IndividualitySignatureActivity.this,individualitySignatureBean.errorMessage,Toast.LENGTH_SHORT).show();
                                }
                            });
                        }
                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }
}
